package com.koron.css2.newProjectCenter.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.mapper.BusinessAreaMapper;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.clientCenter.queryBean.UserInfoQueryBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

public class PjGetUserInfo implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            UserInfoQueryBean userInfoQueryBean = JsonUtils.objectToPojo(req.getData(),UserInfoQueryBean.class);
            UserInfoMapper userInfoMapper = factory.getMapper(UserInfoMapper.class);

            // 1. 分页数据
            PageHelper.startPage(userInfoQueryBean.getPage(),userInfoQueryBean.getPageCount());
            // 2. 查出所有的数据
            List<UserBean> userBeanList = userInfoMapper.getList(userInfoQueryBean, UserBean.USER);
            for (UserBean userBean:userBeanList) {
                // 2.1 businessArea -> businessAreaName
                String businessArea = userBean.getBusinessArea();
                if (StringUtils.isNotEmpty(businessArea)) {
                    BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);
                    String area = businessAreaMapper.findBusinessAreaById(userBean.getBusinessArea(), userInfo.getCurWaterCode());
                    userBean.setBusinessAreaName(area);
                }
            }
            // 3. 返回数据
            MessageBean<PageInfo> messageBean = MessageBean.create(Constant.MESSAGE_INT_SUCCESS,"分户数据查询成功", PageInfo.class);
            messageBean.setData(new PageInfo(userBeanList));

            return messageBean;
        } catch (Exception e) {
            logger.error("分户数据查询失败",e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL,"分户数据查询失败",null);
        }
    }
}
